package com.example.mymicaihu.fragment.home;

import android.content.Context;
import android.graphics.Outline;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.mymicaihu.R;
import com.example.mymicaihu.adapter.home.GameAdapter1;
import com.example.mymicaihu.adapter.home.GameAdapter2;
import com.example.mymicaihu.adapter.home.GameAdapter3;
import com.example.mymicaihu.bean.GameBean;
import com.example.mymicaihu.callback.Mainconcations;
import com.example.mymicaihu.model.MainModel;
import com.example.mymicaihu.presenter.MainPresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：朱勇闯
 * data : 2021/3/29  14:30
 * 作用： xxxx
 */
public class GameFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView {
    private static final String TAG = "朱勇闯Game";
    private List<GameBean.ClassListBean> classList;
    private List<GameBean.GameListBean> gameListClass;
    private List<GameBean.GameListBean> list;
    private List<GameBean.GameListBean> gameList;
    private List<GameBean.GameBannerListBean> bannerList;
    @BindView(R.id.rv_fragment_game_1)
    RecyclerView rv_list1;
    @BindView(R.id.rv_fragment_game_2)
    RecyclerView rv_list2;
    @BindView(R.id.rv_fragment_game_3)
    RecyclerView rv_list3;
    @BindView(R.id.banner_fragment_game)
    Banner banner_game;
    private GameAdapter1 gameAdapter1;
    private GameAdapter2 gameAdapter2;
    private GameAdapter3 gameAdapter3;

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        gameList = new ArrayList<>();
        classList = new ArrayList<>();
        bannerList = new ArrayList<>();
        gameListClass = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_list1.setLayoutManager(manager);
        rv_list2.setLayoutManager(new LinearLayoutManager(context));
        rv_list3.setLayoutManager(new LinearLayoutManager(context));
        gameAdapter1 = new GameAdapter1(classList, context, R.layout.item_game_class);
        gameAdapter2 = new GameAdapter2(gameListClass, context, R.layout.item_game_class_game);
        gameAdapter3 = new GameAdapter3(gameList, context, R.layout.item_game_class_game2);
        rv_list1.setAdapter(gameAdapter1);
        rv_list2.setAdapter(gameAdapter2);
        rv_list3.setAdapter(gameAdapter3);
        HashMap<String, Object> map = new HashMap<>();
        map.put("cv", "2.5.3");
//        mapTab.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg");
        map.put("ms", "MSMyLjUuMyMyMDIwMTIwMSNtYXJrZXRfdml2byMxI3Zpdm92aXZvIFg3I2ZmZmZmZmZmLWU4ZTQtMjIzZC1mZmZmLWZmZmZlNjRiYzYzYg==");
        map.put("referer", "http://api.micaiying.com");
        mPresenter.getData(map, "http://api.micaiying.com/", "game/gameCenter", 0);

    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_home_game;
    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
           /* Gson gson = new Gson();
            Type type1 = new TypeToken<List<VideoBean.NewsListDTO>>() {
            }.getType();
            videoList = gson.fromJson((JsonElement) t, type1);*/

            GameBean gameBean = new Gson().fromJson((JsonElement) t, GameBean.class);
            classList = gameBean.getClassList();
            gameAdapter1 = new GameAdapter1(classList, context, R.layout.item_game_class);
            rv_list1.setAdapter(gameAdapter1);
            gameAdapter1.notifyDataSetChanged();

            //banner
            bannerList = gameBean.getGameBannerList();
            ArrayList<String> ImageUrls = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                ImageUrls.add(bannerList.get(i).getThumb());
            }
            banner_game.setImages(ImageUrls)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(context).load(path).into(imageView);
                        }
                    }).setIndicatorGravity(BannerConfig.RIGHT)
                    .setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(int position) {
                            Toast.makeText(mActivity, "这是" + position + "调数据", Toast.LENGTH_SHORT).show();
                        }
                    }).start();
            banner_game.setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
                }
            });
            banner_game.setClipToOutline(true);


            //game_class_game
            list = gameBean.getGameList();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getType().equals("2")) {
                    gameListClass.add(list.get(i));
                } else if (list.get(i).getType().equals("1")) {
                    gameList.add(list.get(i));
                }
            }
            gameAdapter2 = new GameAdapter2(gameListClass, context, R.layout.item_game_class_game);
            gameAdapter3 = new GameAdapter3(gameList, context, R.layout.item_game_class_game2);
            rv_list2.setAdapter(gameAdapter2);
            gameAdapter2.notifyDataSetChanged();
            rv_list3.setAdapter(gameAdapter3);
            gameAdapter3.notifyDataSetChanged();

        }
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}
